package com.jk.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.RecordDao;
import com.jk.pojo.RecordBean;
import com.jk.utils.ReturnMsgUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RecordService
 * @Descripttion TODO
 * @Author DELL
 * @Date 2022/10/12 20:07
 * @Version 0.001
 */
@Service
public class RecordService {

    @Autowired
    private RecordDao recordDao;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private RestHighLevelClient client;

    /**
     * @Author: binzi
     * @Description:新增es
     * @Date: 2022/10/12 20:18
     * @Return: java.util.Map
     **/
    public Map findRecord(RecordBean bean) {
        String index = "record";
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        boolean exists = false;
        try {
            exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
            if (exists == false){
                QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
                List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
                recordBeans.forEach(RecordBean->{
                    elasticsearchRestTemplate.save(RecordBean);
                });
                return ReturnMsgUtils.returnMsg(200,"这是查询mqsql新增es",recordBeans);
            }else {
                //new 一个list 存放之后查出来的数据
                List<RecordBean> list = new ArrayList<>();
                //公共的查询条件
                BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
                //判断条查的名字是否有值
                if (bean.getRecord_name() != null && bean.getRecord_name() != ""){
                    boolQueryBuilder.must(QueryBuilders.matchQuery("record_name",bean.getRecord_name()));
                }
                //时间区间查
                if (bean.getStaterDate() != null && bean.getStaterDate() != "" && bean.getEndDate() != null && bean.getEndDate() != ""){
                    boolQueryBuilder.must(QueryBuilders.rangeQuery("record_date").lte(bean.getEndDate()).gte(bean.getStaterDate()));
                }
                //服务包精确查
                if (bean.getRecord_server() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_server",bean.getRecord_server()));
                }
                //签约状态精确查
                if (bean.getRecord_state() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_state",bean.getRecord_state()));
                }
                //医生团队精确查
                if (bean.getRecord_doctor_team() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_doctor_team",bean.getRecord_doctor_team()));
                }
                //签约机构精确查
                if (bean.getRecord_organization() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_organization",bean.getRecord_organization()));
                }

                /*设置高亮字段*/
                HighlightBuilder highlightBuilder = new HighlightBuilder();
                //实体类要高亮的value字段
                highlightBuilder.field("record_name");
                highlightBuilder.preTags("<font color='red'>");
                highlightBuilder.postTags("</font>");
                System.out.println("高亮-----"+highlightBuilder);
                //es中的业务
                NativeSearchQuery query = new NativeSearchQueryBuilder()
                        //查询条件
                        .withQuery(boolQueryBuilder)
                        //高亮
                        .withHighlightBuilder(highlightBuilder)
                        .build();
                query.setMaxResults(50);
                SearchHits<RecordBean> search = elasticsearchRestTemplate.search(query, RecordBean.class);

                //得到es查询返回的内容 hit
                List<SearchHit<RecordBean>> searchHits = search.getSearchHits();

                //循环searchHits
                searchHits.forEach(hit->{
                    //获取高亮的内容
                    Map<String, List<String>> highlightFields = hit.getHighlightFields();
                    //判断高亮的字段是否有值
                    hit.getContent().setRecord_name(highlightFields.get("record_name") == null ? hit.getContent().getRecord_name():highlightFields.get("record_name").get(0));
                    list.add(hit.getContent());
                });

                return ReturnMsgUtils.returnMsg(201,"这是es中的数据",list);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    /**
     * @Author: binzi
     * @Description:新增mysql同步到es中
     * @Date: 2022/10/13 11:34
     * @Return: void
     **/
    public void addRecord(RecordBean recordBean) {
        recordDao.insert(recordBean);
        QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
        List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
        recordBeans.forEach(RecordBean->{
            elasticsearchRestTemplate.save(RecordBean);
        });
    }

    /**
     * @Author: binzi
     * @Description:根据ID删除
     * @Date: 2022/10/13 16:12
     * @Return: boolean
     **/
    public boolean delRecordById(Integer record_id) {
        try {
            recordDao.deleteById(record_id);
            elasticsearchRestTemplate.delete(record_id.toString(), IndexCoordinates.of("record"));
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @Author: binzi
     * @Description:根据pid回显
     * @Date: 2022/10/13 17:22
     * @Return: com.jk.pojo.RecordBean
     **/
    public List<RecordBean> findRecordByPid(Integer pid) {
        return recordDao.findRecordByPid(pid);

    }

    /**
     * @Author: binzi
     * @Description:查询当前时间一周的数据
     * @Date: 2022/10/17 15:15
     * @Return: java.util.List<com.jk.pojo.RecordBean>
     **/
    public Map findDateByWeek() {
        List<Integer> dateByWeek = recordDao.findDateByWeek();
        return ReturnMsgUtils.returnMsg(200,"1111",dateByWeek);
    }

    /**
     * @Author: binzi
     * @Description:已完成服务数量
     * @Date: 2022/10/18 10:54
     * @Return: java.lang.Integer
     **/
    public Integer findRecordByStateCount() {
        return recordDao.findRecordByStateCount();
    }

    /**
     * @Author: binzi
     * @Description:根据id(编号)回显
     * @Date: 2022/10/19 9:26
     * @Return: com.jk.pojo.RecordBean
     **/
    public RecordBean findRecordById(Integer record_id) {
        //索引名称
        String index = "record";
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        boolean exists = false;
        //判断es中索引是否存在 存在true,不存在false
        try {//true
            exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
            if (exists == false){
                    return recordDao.selectById(record_id);
            }else {
                IdsQueryBuilder ids = QueryBuilders.idsQuery();
                ids.addIds(record_id.toString());
                NativeSearchQuery build = new NativeSearchQueryBuilder()
                        .withQuery(ids)
                        .build();
                SearchHit<RecordBean> recordBeanSearchHit = elasticsearchRestTemplate.searchOne(build, RecordBean.class);
                if (recordBeanSearchHit != null){
                    RecordBean content = recordBeanSearchHit.getContent();
                    return content;
                }else {
                    return null;
                }
            }
        } catch (IOException e) {//false
            e.printStackTrace();
            return null;
        }
    }
    /**
     * @Author 邓金康
     * @Description //编辑修改
     * @Date  14:04
     * @param recordBean:
     * @return: void
     **/
    public void findRecordModify(RecordBean recordBean) throws IOException {
        recordDao.updateById(recordBean);
        // 定义索引名称
        DeleteIndexRequest request = new DeleteIndexRequest("record");
        // 发送请求到ES
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
    }

    /**
     * @Author: binzi
     * @Description:查询服务 状态为1的
     * @Date: 2022/10/23 19:18
     * @Return: java.util.Map
     **/
    public Map findRecord1(RecordBean bean) {
        String index = "record";
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        boolean exists = false;
        try {
            exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
            if (exists == false){
                QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
                List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
                recordBeans.forEach(RecordBean->{
                    elasticsearchRestTemplate.save(RecordBean);
                });
                return ReturnMsgUtils.returnMsg(200,"这是查询mqsql新增es",recordBeans);
            }else {
                //new 一个list 存放之后查出来的数据
                List<RecordBean> list = new ArrayList<>();
                //公共的查询条件
                BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
                //签约状态精确查
                boolQueryBuilder.must(QueryBuilders.termQuery("record_state", 1));
                //判断条查的名字是否有值
                if (bean.getRecord_name() != null && bean.getRecord_name() != ""){
                    boolQueryBuilder.must(QueryBuilders.matchQuery("record_name",bean.getRecord_name()));
                }
                //时间区间查
                if (bean.getStaterDate() != null && bean.getStaterDate() != "" && bean.getEndDate() != null && bean.getEndDate() != ""){
                    boolQueryBuilder.must(QueryBuilders.rangeQuery("record_date").lte(bean.getEndDate()).gte(bean.getStaterDate()));
                }
                //服务包精确查
                if (bean.getRecord_server() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_server",bean.getRecord_server()));
                }

                //医生团队精确查
                if (bean.getRecord_doctor_team() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_doctor_team",bean.getRecord_doctor_team()));
                }
                //签约机构精确查
                if (bean.getRecord_organization() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_organization",bean.getRecord_organization()));
                }

                /*设置高亮字段*/
                HighlightBuilder highlightBuilder = new HighlightBuilder();
                //实体类要高亮的value字段
                highlightBuilder.field("record_name");
                highlightBuilder.preTags("<font color='red'>");
                highlightBuilder.postTags("</font>");
                System.out.println("高亮-----"+highlightBuilder);
                //es中的业务
                NativeSearchQuery query = new NativeSearchQueryBuilder()
                        //查询条件
                        .withQuery(boolQueryBuilder)
                        //高亮
                        .withHighlightBuilder(highlightBuilder)
                        .build();
                query.setMaxResults(50);
                SearchHits<RecordBean> search = elasticsearchRestTemplate.search(query, RecordBean.class);

                //得到es查询返回的内容 hit
                List<SearchHit<RecordBean>> searchHits = search.getSearchHits();

                //循环searchHits
                searchHits.forEach(hit->{
                    //获取高亮的内容
                    Map<String, List<String>> highlightFields = hit.getHighlightFields();
                    //判断高亮的字段是否有值
                    hit.getContent().setRecord_name(highlightFields.get("record_name") == null ? hit.getContent().getRecord_name():highlightFields.get("record_name").get(0));
                    list.add(hit.getContent());
                });

                return ReturnMsgUtils.returnMsg(201,"这是es中的数据",list);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    /**
     * @Author: binzi
     * @Description:查询服务 状态为2的
     * @Date: 2022/10/23 19:26
     * @Return: java.util.Map
     **/
    public Map findRecord2(RecordBean bean) {
        String index = "record";
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        boolean exists = false;
        try {
            exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
            if (exists == false){
                QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
                List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
                recordBeans.forEach(RecordBean->{
                    elasticsearchRestTemplate.save(RecordBean);
                });
                return ReturnMsgUtils.returnMsg(200,"这是查询mqsql新增es",recordBeans);
            }else {
                //new 一个list 存放之后查出来的数据
                List<RecordBean> list = new ArrayList<>();
                //公共的查询条件
                BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
                //签约状态精确查
                boolQueryBuilder.must(QueryBuilders.termQuery("record_state", 2));
                //判断条查的名字是否有值
                if (bean.getRecord_name() != null && bean.getRecord_name() != ""){
                    boolQueryBuilder.must(QueryBuilders.matchQuery("record_name",bean.getRecord_name()));
                }
                //时间区间查
                if (bean.getStaterDate() != null && bean.getStaterDate() != "" && bean.getEndDate() != null && bean.getEndDate() != ""){
                    boolQueryBuilder.must(QueryBuilders.rangeQuery("record_date").lte(bean.getEndDate()).gte(bean.getStaterDate()));
                }
                //服务包精确查
                if (bean.getRecord_server() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_server",bean.getRecord_server()));
                }

                //医生团队精确查
                if (bean.getRecord_doctor_team() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_doctor_team",bean.getRecord_doctor_team()));
                }
                //签约机构精确查
                if (bean.getRecord_organization() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_organization",bean.getRecord_organization()));
                }

                /*设置高亮字段*/
                HighlightBuilder highlightBuilder = new HighlightBuilder();
                //实体类要高亮的value字段
                highlightBuilder.field("record_name");
                highlightBuilder.preTags("<font color='red'>");
                highlightBuilder.postTags("</font>");
                System.out.println("高亮-----"+highlightBuilder);
                //es中的业务
                NativeSearchQuery query = new NativeSearchQueryBuilder()
                        //查询条件
                        .withQuery(boolQueryBuilder)
                        //高亮
                        .withHighlightBuilder(highlightBuilder)

                        .build();
                query.setMaxResults(50);
                SearchHits<RecordBean> search = elasticsearchRestTemplate.search(query, RecordBean.class);

                //得到es查询返回的内容 hit
                List<SearchHit<RecordBean>> searchHits = search.getSearchHits();

                //循环searchHits
                searchHits.forEach(hit->{
                    //获取高亮的内容
                    Map<String, List<String>> highlightFields = hit.getHighlightFields();
                    //判断高亮的字段是否有值
                    hit.getContent().setRecord_name(highlightFields.get("record_name") == null ? hit.getContent().getRecord_name():highlightFields.get("record_name").get(0));
                    list.add(hit.getContent());
                });

                return ReturnMsgUtils.returnMsg(201,"这是es中的数据",list);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }


    /**
     * @Author: binzi
     * @Description:根据id更改状态
     * @Date: 2022/10/23 19:34
     * @Return: void
     **/
    public void updateStateById(Integer record_id) throws IOException {
        recordDao.updateStateById(record_id);
        // 定义索引名称
        DeleteIndexRequest request = new DeleteIndexRequest("record");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        //同步
        QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
        List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
        recordBeans.forEach(RecordBean->{
            elasticsearchRestTemplate.save(RecordBean);
        });
    }

    /**
     * @Author: binzi
     * @Description:根据id更改状态
     * @Date: 2022/10/23 19:34
     * @Return: void
     **/
    public void updateStateById2(Integer record_id) throws IOException {
        recordDao.updateStateById2(record_id);
        // 定义索引名称
        DeleteIndexRequest request = new DeleteIndexRequest("record");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        //同步
        QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
        List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
        recordBeans.forEach(RecordBean->{
            elasticsearchRestTemplate.save(RecordBean);
        });
    }

    /**
     * @Author: binzi
     * @Description:查询服务 状态为3的
     * @Date: 2022/10/23 20:15
     * @Return: java.util.Map
     **/
    public Map findRecord3(RecordBean bean) {
        String index = "record";
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        boolean exists = false;
        try {
            exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
            if (exists == false){
                QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
                List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
                recordBeans.forEach(RecordBean->{
                    elasticsearchRestTemplate.save(RecordBean);
                });
                return ReturnMsgUtils.returnMsg(200,"这是查询mqsql新增es",recordBeans);
            }else {
                //new 一个list 存放之后查出来的数据
                List<RecordBean> list = new ArrayList<>();
                //公共的查询条件
                BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
                //签约状态精确查
                boolQueryBuilder.must(QueryBuilders.termQuery("record_state", 3));
                //判断条查的名字是否有值
                if (bean.getRecord_name() != null && bean.getRecord_name() != ""){
                    boolQueryBuilder.must(QueryBuilders.matchQuery("record_name",bean.getRecord_name()));
                }
                //时间区间查
                if (bean.getStaterDate() != null && bean.getStaterDate() != "" && bean.getEndDate() != null && bean.getEndDate() != ""){
                    boolQueryBuilder.must(QueryBuilders.rangeQuery("record_date").lte(bean.getEndDate()).gte(bean.getStaterDate()));
                }
                //服务包精确查
                if (bean.getRecord_server() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_server",bean.getRecord_server()));
                }

                //医生团队精确查
                if (bean.getRecord_doctor_team() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_doctor_team",bean.getRecord_doctor_team()));
                }
                //签约机构精确查
                if (bean.getRecord_organization() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_organization",bean.getRecord_organization()));
                }

                /*设置高亮字段*/
                HighlightBuilder highlightBuilder = new HighlightBuilder();
                //实体类要高亮的value字段
                highlightBuilder.field("record_name");
                highlightBuilder.preTags("<font color='red'>");
                highlightBuilder.postTags("</font>");
                System.out.println("高亮-----"+highlightBuilder);
                //es中的业务
                NativeSearchQuery query = new NativeSearchQueryBuilder()
                        //查询条件
                        .withQuery(boolQueryBuilder)
                        //高亮
                        .withHighlightBuilder(highlightBuilder)

                        .build();
                query.setMaxResults(50);
                SearchHits<RecordBean> search = elasticsearchRestTemplate.search(query, RecordBean.class);

                //得到es查询返回的内容 hit
                List<SearchHit<RecordBean>> searchHits = search.getSearchHits();

                //循环searchHits
                searchHits.forEach(hit->{
                    //获取高亮的内容
                    Map<String, List<String>> highlightFields = hit.getHighlightFields();
                    //判断高亮的字段是否有值
                    hit.getContent().setRecord_name(highlightFields.get("record_name") == null ? hit.getContent().getRecord_name():highlightFields.get("record_name").get(0));
                    list.add(hit.getContent());
                });

                return ReturnMsgUtils.returnMsg(201,"这是es中的数据",list);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    /**
     * @Author: binzi
     * @Description:查询服务 状态为3的
     * @Date: 2022/10/23 20:16
     * @Return: java.util.Map
     **/
    public Map findRecord4(RecordBean bean) {
        String index = "record";
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        boolean exists = false;
        try {
            exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
            if (exists == false){
                QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
                List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
                recordBeans.forEach(RecordBean->{
                    elasticsearchRestTemplate.save(RecordBean);
                });
                return ReturnMsgUtils.returnMsg(200,"这是查询mqsql新增es",recordBeans);
            }else {
                //new 一个list 存放之后查出来的数据
                List<RecordBean> list = new ArrayList<>();
                //公共的查询条件
                BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
                //签约状态精确查
                boolQueryBuilder.must(QueryBuilders.termQuery("record_state", 4));
                //判断条查的名字是否有值
                if (bean.getRecord_name() != null && bean.getRecord_name() != ""){
                    boolQueryBuilder.must(QueryBuilders.matchQuery("record_name",bean.getRecord_name()));
                }
                //时间区间查
                if (bean.getStaterDate() != null && bean.getStaterDate() != "" && bean.getEndDate() != null && bean.getEndDate() != ""){
                    boolQueryBuilder.must(QueryBuilders.rangeQuery("record_date").lte(bean.getEndDate()).gte(bean.getStaterDate()));
                }
                //服务包精确查
                if (bean.getRecord_server() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_server",bean.getRecord_server()));
                }

                //医生团队精确查
                if (bean.getRecord_doctor_team() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_doctor_team",bean.getRecord_doctor_team()));
                }
                //签约机构精确查
                if (bean.getRecord_organization() != null){
                    boolQueryBuilder.must(QueryBuilders.termQuery("record_organization",bean.getRecord_organization()));
                }

                /*设置高亮字段*/
                HighlightBuilder highlightBuilder = new HighlightBuilder();
                //实体类要高亮的value字段
                highlightBuilder.field("record_name");
                highlightBuilder.preTags("<font color='red'>");
                highlightBuilder.postTags("</font>");
                System.out.println("高亮-----"+highlightBuilder);
                //es中的业务
                NativeSearchQuery query = new NativeSearchQueryBuilder()
                        //查询条件
                        .withQuery(boolQueryBuilder)
                        //高亮
                        .withHighlightBuilder(highlightBuilder)

                        .build();
                query.setMaxResults(50);
                SearchHits<RecordBean> search = elasticsearchRestTemplate.search(query, RecordBean.class);

                //得到es查询返回的内容 hit
                List<SearchHit<RecordBean>> searchHits = search.getSearchHits();

                //循环searchHits
                searchHits.forEach(hit->{
                    //获取高亮的内容
                    Map<String, List<String>> highlightFields = hit.getHighlightFields();
                    //判断高亮的字段是否有值
                    hit.getContent().setRecord_name(highlightFields.get("record_name") == null ? hit.getContent().getRecord_name():highlightFields.get("record_name").get(0));
                    list.add(hit.getContent());
                });

                return ReturnMsgUtils.returnMsg(201,"这是es中的数据",list);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    /**
     * @Author: binzi
     * @Description:根据id更改状态 4
     * @Date: 2022/10/23 19:34
     * @Return: void
     **/
    public void updateStateById3(Integer record_id) throws IOException {
        recordDao.updateStateById3(record_id);
        // 定义索引名称
        DeleteIndexRequest request = new DeleteIndexRequest("record");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        //同步
        QueryWrapper<RecordBean> recordBeanQueryWrapper = new QueryWrapper<RecordBean>();
        List<RecordBean> recordBeans = recordDao.selectList(recordBeanQueryWrapper);
        recordBeans.forEach(RecordBean->{
            elasticsearchRestTemplate.save(RecordBean);
        });
    }
}
